package com.nkym.wypan.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.nkym.wypan.enums.ResponseStatus;
import com.nkym.wypan.exception.BusinessException;
import com.nkym.wypan.mapper.UserInfoMapper;
import com.nkym.wypan.model.entity.UserInfo;
import com.nkym.wypan.model.req.userinfo.AddTotalSpaceReq;
import com.nkym.wypan.service.UserInfoService;
import com.nkym.wypan.utils.UserContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 19637
 * @description 针对表【wy_pan_user_info】的数据库操作Service实现
 * @createDate 2023-06-07 15:42:47
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
        implements UserInfoService {
    @Resource
    UserInfoMapper userInfoMapper;

    @Override
    public void addTotalSpace(AddTotalSpaceReq addTotalSpaceReq) {
        Long addSpace = addTotalSpaceReq.getAddSpace();
        Long userId = addTotalSpaceReq.getUserId();
        //todo 仿流量盗刷
        UserInfo userInfo = userInfoMapper.selectById(userId);
        if (userInfo == null) {
            throw new BusinessException(ResponseStatus.USER_NOT_EXIST);
        }
        userInfoMapper.addTotalSpace(userId, addSpace);
    }

    @Override
    public void addUsedSpace(Long size) {
        Long uid = UserContextHolder.getId();
        userInfoMapper.addUseSpace(size, uid);
    }

    @Override
    public void freeUpUseSpace(Long size) {
        Long uid = UserContextHolder.getId();
        userInfoMapper.freeUpUseSpace(size, uid);
    }
}




